﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class CustomerDetail : Form
    {
        private int id;
        public CustomerDetail()
        {
            InitializeComponent();
            if (LoginDao.adminFlag)
            {
                clCancel.Visible = true;
            }
            else
            {
                clCancel.Visible = false;
            }
        }
        public CustomerDetail(int id)
        {
            InitializeComponent();
            this.id = id;
            onload(id);
            if (LoginDao.adminFlag)
            {
                clCancel.Visible = true;
            }
            else
            {
                clCancel.Visible = false;
            }
        }
        private void onload(int id)
        {
            PurchaseDao dao = new PurchaseDao();
            DataRow row = dao.getCustomerHist(id);
            if (row != null)
            {
                txtName.Text = row["NAME"].ToString();
                rtbNote.Text = row["NOTE"].ToString();
                loadImageCapture(row["PICTURE"].ToString());
                txtMonth.Text = row["TOTAL_MONTH"].ToString();
            }
            DataTable table = dao.getCustommerPurchaseHist(id);
            dtgCustomers.Rows.Clear();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtgCustomers.Rows.Add();
                    dtgCustomers.Rows[i].Cells[0].Value = i + 1;
                    dtgCustomers.Rows[i].Cells[1].Value = DateTime.Parse(row1["CREATE_DATE"].ToString()).ToString("dd/MM/yyyy");
                    dtgCustomers.Rows[i].Cells[2].Value = DateTime.Parse(row1["PURCHASE_DATE"].ToString()).ToString("dd/MM/yyyy");
                    dtgCustomers.Rows[i].Cells[3].Value = row1["TOTAL_MONTH"].ToString();
                    dtgCustomers.Rows[i].Cells[4].Value = double.Parse(row1["TOTAL_AMOUN"].ToString()).ToString("#,###.##");
                    dtgCustomers.Rows[i].Cells[6].Value = row1["PURCHASE_ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
        }
        public void loadImageCapture(string filename)
        {
            string path = filename;
            if (!String.IsNullOrEmpty(path))
            {
                try
                {
                    ptbImage.LoadAsync(path);
                    ptbImage.SizeMode = PictureBoxSizeMode.Zoom;
                    ptbImage.Text = filename;
                }
                catch (Exception ex)
                {
                    ptbImage.Image = null;
                    Console.WriteLine(ex);
                }
            }
            else
            {
                ptbImage.Image = null;
            }
        }

        private void dtgCustomers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dtgCustomers_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 5)
            {
                int idii = int.Parse((dtgCustomers.Rows[e.RowIndex].Cells[6].Value == null || dtgCustomers.Rows[e.RowIndex].Cells[6].Value.ToString() == "") ? "0" :  dtgCustomers.Rows[e.RowIndex].Cells[6].Value.ToString()) ;
                CancelPurchase can = new CancelPurchase(idii);
                can.ShowDialog();
                onload(id);
            }
        }
    }
}
